package com.ems.dao;

import com.ems.model.Employee;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDAO {
    // 添加员工
    public void addEmployee(Employee employee) throws SQLException {
        String sql = "INSERT INTO employees (name, gender, birth_date, phone, email, position, hire_date, department_id) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, employee.getName());
            stmt.setString(2, employee.getGender());
            stmt.setDate(3, new java.sql.Date(employee.getBirthDate().getTime()));
            stmt.setString(4, employee.getPhone());
            stmt.setString(5, employee.getEmail());
            stmt.setString(6, employee.getPosition());
            stmt.setDate(7, new java.sql.Date(employee.getHireDate().getTime()));
            stmt.setInt(8, employee.getDepartmentId());
            stmt.executeUpdate();
        }
    }

    // 删除员工
    public void deleteEmployee(int id) throws SQLException {
        String sql = "DELETE FROM employees WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        }
    }

    // 更新员工信息
    public void updateEmployee(Employee employee) throws SQLException {
        String sql = "UPDATE employees SET name=?, gender=?, birth_date=?, phone=?, email=?, " +
                "position=?, hire_date=?, department_id=? WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, employee.getName());
            stmt.setString(2, employee.getGender());
            stmt.setDate(3, new java.sql.Date(employee.getBirthDate().getTime()));
            stmt.setString(4, employee.getPhone());
            stmt.setString(5, employee.getEmail());
            stmt.setString(6, employee.getPosition());
            stmt.setDate(7, new java.sql.Date(employee.getHireDate().getTime()));
            stmt.setInt(8, employee.getDepartmentId());
            stmt.setInt(9, employee.getId());
            stmt.executeUpdate();
        }
    }

    // 获取所有员工
    public List<Employee> getAllEmployees() throws SQLException {
        List<Employee> employees = new ArrayList<>();
        String sql = "SELECT * FROM employees";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Employee emp = new Employee();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setGender(rs.getString("gender"));
                emp.setBirthDate(rs.getDate("birth_date"));
                emp.setPhone(rs.getString("phone"));
                emp.setEmail(rs.getString("email"));
                emp.setPosition(rs.getString("position"));
                emp.setHireDate(rs.getDate("hire_date"));
                emp.setDepartmentId(rs.getInt("department_id"));
                employees.add(emp);
            }
        }
        return employees;
    }

    // 根据ID获取员工
    public Employee getEmployeeById(int id) throws SQLException {
        String sql = "SELECT * FROM employees WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Employee emp = new Employee();
                    emp.setId(rs.getInt("id"));
                    emp.setName(rs.getString("name"));
                    emp.setGender(rs.getString("gender"));
                    emp.setBirthDate(rs.getDate("birth_date"));
                    emp.setPhone(rs.getString("phone"));
                    emp.setEmail(rs.getString("email"));
                    emp.setPosition(rs.getString("position"));
                    emp.setHireDate(rs.getDate("hire_date"));
                    emp.setDepartmentId(rs.getInt("department_id"));
                    return emp;
                }
            }
        }
        return null;
    }
}
